/*
 *
 *  * Copyright (c) 2016. For Intelligent Group.
 *
 */

package com.intelligent.mts.client.core.view;

import com.intelligent.mts.client.core.controller.MainController;
import com.intelligent.mts.client.event.StatusBarChangeEvent;
import com.intelligent.mts.client.listener.SearchDataListener;
import com.intelligent.mts.client.utils.*;
import com.intelligent.mts.common.utils.ApplicationException;
import com.intelligent.mts.common.utils.ExceptionMessages;
import com.intelligent.mts.common.utils.StringUtils;
import com.intelligent.mts.core.dto.ResultDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * Created by Peter on 2016/7/21.
 */
@Component
public class MainSearchPanel extends JPanel {

    @Autowired
    private MainController mainController;

    @PostConstruct
    private void initComponents() {
        EventBusUtils.register(new SearchDataListener());

        lblSn = new JLabel();
        txtSn = new JTextField();
        btnSearch = new JButton();

        lblSn.setText("Serial Number:");
        btnSearch.setText("Search");
        btnSearch.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSearchActionPerformed(evt);
            }
        });
        this.setLayout(new GridBagLayout());
        this.add(lblSn, new GBC(0, 0).setFill(GBC.HORIZONTAL).setWeight(1, 0).setInsets(0, 0, 0, 2));
        this.add(txtSn, new GBC(1, 0).setFill(GBC.HORIZONTAL).setWeight(20, 0).setInsets(0, 0, 0, 2));
        this.add(btnSearch, new GBC(2, 0).setFill(GBC.HORIZONTAL).setWeight(5, 0));
        this.add(new JPanel(), new GBC(3, 0).setFill(GBC.HORIZONTAL).setWeight(50, 0));
    }

    private void btnSearchActionPerformed(ActionEvent evt) {
        String serialValue = getTxtSn().getText();
        serialValue = serialValue.trim();
        searchData(serialValue);
    }
    public void searchData(String serialValue) {
        try {
            if (StringUtils.isBlank(serialValue)) {
                throw new ApplicationException(ExceptionMessages.EXCEPTION_SEARCH_SN_CANNOT_BE_EMPTY);
            }
            ResultDto resultDto = mainController.findAllData(serialValue);

            ApplicationBeanUtils.getBean(MainHistoryPanel.class).reloadTable(resultDto.getHistoryDtoList());
            ApplicationBeanUtils.getBean(MainAttrPanel.class).reloadTable(resultDto.getAttributeList());
            ApplicationBeanUtils.getBean(MainAttrPanel.class).setTextFieldValues(resultDto.getSerialNumberDtoList(), resultDto.getTrackDto(), resultDto.getCustomerName());
            EventBusUtils.post(new StatusBarChangeEvent(ResourceBundleUtils.getString(ActionMessages.SEARCH_DONE)));

        } catch (ApplicationException ex) {
            if(ResourceBundleUtils.getString(ex.getMessage()) == null || "".equals(ResourceBundleUtils.getString(ex.getMessage()))){
                EventBusUtils.post(new StatusBarChangeEvent(ResourceBundleUtils.getString(ExceptionMessages.EXCEPTION_GLOBAL_SYSTERM_ERROR), StatusBarChangeEvent.MSG_LEVEL_ERROR));
            }else
                EventBusUtils.post(new StatusBarChangeEvent(ResourceBundleUtils.getString(ex.getMessage()), StatusBarChangeEvent.MSG_LEVEL_ERROR));
        }
    }

    private JLabel lblSn;
    private JTextField txtSn;
    private JButton btnSearch;

    public JButton getBtnSearch() {
        return btnSearch;
    }

    public JTextField getTxtSn() {
        return txtSn;
    }
}
